## Load Data

source("Code/utility functions.R")
anes2012 <- foreign::read.spss("anes_timeseries_2012.sav",to.data.frame=T)

anes2012$oldnew <- car::recode(as.numeric(anes2012$randsplice_revisedstandard),"1='New';2='Old'")
# there was a mistake in randomization, so a second splice variable for the post-efficacy variable was created
anes2012$oldnew2 <- car::recode(is.na(anes2012$efficpo_complicstd) & is.na(anes2012$efficpo_undstd) & is.na(anes2012$efficpo_carestd) & is.na(anes2012$efficpo_saystd),"TRUE='New';FALSE='Old'")
anes2012$oldnew2[as.numeric(anes2012$wave_completions)!=1]=NA

########## Criterion Variables
########## Criterion Variables
########## Criterion Variables
#########Efficacy Pre
anes2012$efficacy1stdpre <- zero1(as.numeric(anes2012$effic_complicstd))
anes2012$efficacy1revpre <- zero1(as.numeric(anes2012$effic_complicrev))

anes2012$efficacy1pre <- NA 
anes2012$efficacy1pre[which(anes2012$oldnew=="Old")]=anes2012$efficacy1stdpre[which(anes2012$oldnew=="Old")]
anes2012$efficacy1pre[which(anes2012$oldnew=="New")]=anes2012$efficacy1revpre[which(anes2012$oldnew=="New")]

anes2012$efficacy2stdpre <- 1-zero1(as.numeric(anes2012$effic_undstd))
anes2012$efficacy2revpre <- 1-zero1(as.numeric(anes2012$effic_undrev))

anes2012$efficacy2pre <- NA 
anes2012$efficacy2pre[which(anes2012$oldnew=="Old")]=anes2012$efficacy2stdpre[which(anes2012$oldnew=="Old")]
anes2012$efficacy2pre[which(anes2012$oldnew=="New")]=anes2012$efficacy2revpre[which(anes2012$oldnew=="New")]

anes2012$efficacy3stdpre <- zero1(as.numeric(anes2012$effic_carestd))
anes2012$efficacy3revpre <- 1-zero1(as.numeric(anes2012$effic_carerev))

anes2012$efficacy3pre <- NA 
anes2012$efficacy3pre[which(anes2012$oldnew=="Old")]=anes2012$efficacy3stdpre[which(anes2012$oldnew=="Old")]
anes2012$efficacy3pre[which(anes2012$oldnew=="New")]=anes2012$efficacy3revpre[which(anes2012$oldnew=="New")]

anes2012$efficacy4stdpre <- zero1(as.numeric(anes2012$effic_saystd))
anes2012$efficacy4revpre <- 1-zero1(as.numeric(anes2012$effic_sayrev))

anes2012$efficacy4pre <- NA 
anes2012$efficacy4pre[which(anes2012$oldnew=="Old")]=anes2012$efficacy4stdpre[which(anes2012$oldnew=="Old")]
anes2012$efficacy4pre[which(anes2012$oldnew=="New")]=anes2012$efficacy4revpre[which(anes2012$oldnew=="New")]

with(anes2012,cor(data.frame(efficacy4revpre,efficacy3revpre,efficacy2revpre,efficacy1revpre),use="pairwise.complete.obs"))
with(anes2012,cor(data.frame(efficacy4stdpre,efficacy3stdpre,efficacy2stdpre,efficacy1stdpre),use="pairwise.complete.obs"))
#########Efficacy Post
table(anes2012$wave_completions)
anes2012$efficacy1stdpo <- zero1(as.numeric(anes2012$efficpo_complicstd))
anes2012$efficacy1revpo <- zero1(as.numeric(anes2012$efficpo_complicrev))

anes2012$efficacy1po <- NA 
anes2012$efficacy1po[which(anes2012$oldnew2=="Old")]=anes2012$efficacy1stdpo[which(anes2012$oldnew2=="Old")]
anes2012$efficacy1po[which(anes2012$oldnew2=="New")]=anes2012$efficacy1revpo[which(anes2012$oldnew2=="New")]


anes2012$efficacy2stdpo <- 1-zero1(as.numeric(anes2012$efficpo_undstd))
anes2012$efficacy2revpo <- 1-zero1(as.numeric(anes2012$efficpo_undrev))

anes2012$efficacy2po <- NA 
anes2012$efficacy2po[which(anes2012$oldnew2=="Old")]=anes2012$efficacy2stdpo[which(anes2012$oldnew2=="Old")]
anes2012$efficacy2po[which(anes2012$oldnew2=="New")]=anes2012$efficacy2revpo[which(anes2012$oldnew2=="New")]

#########

anes2012$efficacy3stdpo <- zero1(as.numeric(anes2012$efficpo_carestd))
anes2012$efficacy3revpo <- 1-zero1(as.numeric(anes2012$efficpo_carerev))

anes2012$efficacy3po <- NA 
anes2012$efficacy3po[which(anes2012$oldnew2=="Old")]=anes2012$efficacy3stdpo[which(anes2012$oldnew2=="Old")]
anes2012$efficacy3po[which(anes2012$oldnew2=="New")]=anes2012$efficacy3revpo[which(anes2012$oldnew2=="New")]

anes2012$efficacy4stdpo <- zero1(as.numeric(anes2012$efficpo_saystd))
anes2012$efficacy4revpo <- 1-zero1(as.numeric(anes2012$efficpo_sayrev))

anes2012$efficacy4po <- NA 
anes2012$efficacy4po[which(anes2012$oldnew2=="Old")]=anes2012$efficacy4stdpo[which(anes2012$oldnew2=="Old")]
anes2012$efficacy4po[which(anes2012$oldnew2=="New")]=anes2012$efficacy4revpo[which(anes2012$oldnew2=="New")]

anes2012$revisedpo_internal <- with(anes2012,rowMeans(data.frame(efficacy2revpo,efficacy1revpo)),na.rm=T)
anes2012$revisedpre_internal <- with(anes2012,rowMeans(data.frame(efficacy2revpre,efficacy1revpre)),na.rm=T)
anes2012$revisedpo_external <- with(anes2012,rowMeans(data.frame(efficacy3revpo,efficacy4revpo)),na.rm=T)
anes2012$revisedpre_external <- with(anes2012,rowMeans(data.frame(efficacy3revpre,efficacy4revpre)),na.rm=T)

anes2012$stdpo_internal <- with(anes2012,rowMeans(data.frame(efficacy2stdpo,efficacy1stdpo)),na.rm=T)
anes2012$stdpre_internal <- with(anes2012,rowMeans(data.frame(efficacy2stdpre,efficacy1stdpre)),na.rm=T)
anes2012$stdpo_external <- with(anes2012,rowMeans(data.frame(efficacy3stdpo,efficacy4stdpo)),na.rm=T)
anes2012$stdpre_external <- with(anes2012,rowMeans(data.frame(efficacy3stdpre,efficacy4stdpre)),na.rm=T)

anes2012$po_external <- NA
anes2012$po_external[which(anes2012$oldnew2=="Old")]=anes2012$stdpo_external[which(anes2012$oldnew2=="Old")]
anes2012$po_external[which(anes2012$oldnew2=="New")]=anes2012$revisedpo_external[which(anes2012$oldnew2=="New")]
anes2012$pre_external <- NA
anes2012$pre_external[which(anes2012$oldnew=="Old")]=anes2012$stdpre_external[which(anes2012$oldnew=="Old")]
anes2012$pre_external[which(anes2012$oldnew=="New")]=anes2012$revisedpre_external[which(anes2012$oldnew=="New")]

########
anes2012$po_internal <- NA
anes2012$po_internal[which(anes2012$oldnew2=="Old")]=anes2012$stdpo_internal[which(anes2012$oldnew2=="Old")]
anes2012$po_internal[which(anes2012$oldnew2=="New")]=anes2012$revisedpo_internal[which(anes2012$oldnew2=="New")]

anes2012$pre_internal <- NA
anes2012$pre_internal[which(anes2012$oldnew=="Old")]=anes2012$stdpre_internal[which(anes2012$oldnew=="Old")]
anes2012$pre_internal[which(anes2012$oldnew=="New")]=anes2012$revisedpre_internal[which(anes2012$oldnew=="New")]





with(anes2012,cor(data.frame(efficacy4revpo,efficacy3revpo,efficacy2revpo,efficacy1revpo),use="pairwise.complete.obs"))

with(anes2012,cor(data.frame(efficacy4stdpo,efficacy3stdpo,efficacy2stdpo,efficacy1stdpo),use="pairwise.complete.obs"))


with(anes2012,cor(data.frame(efficacy4stdpre,efficacy3stdpre,efficacy2stdpre,efficacy1stdpre),use="pairwise.complete.obs"))

########## Target Variables
########## Target Variables
########## Target Variables

############# Political Activities
a <- car::recode(as.numeric(anes2012$dhsinvolv_march),"1=1;2=0;else=NA")
b <- car::recode(as.numeric(anes2012$dhsinvolv_netpetition),"1=1;2=0;else=NA")
c <- car::recode(as.numeric(anes2012$dhsinvolv_petition),"1=1;2=0;else=NA")
d <- car::recode(as.numeric(anes2012$dhsinvolv_relig),"1=1;2=0;else=NA")
e <-  car::recode(as.numeric(anes2012$dhsinvolv_org),"1=1;2=0;else=NA")
f <-  car::recode(as.numeric(anes2012$dhsinvolv_call),"1=1;2=0;else=NA")
g <-  car::recode(as.numeric(anes2012$dhsinvolv_message),"1=1;2=0;else=NA")
h <-  car::recode(as.numeric(anes2012$dhsinvolv_letter),"1=1;2=0;else=NA")
i <-  car::recode(as.numeric(anes2012$dhsinvolv_contact1),"1=1;2=0;else=NA")
anes2012$polactivity <- zero1(rowSums(data.frame(a,b,c,d,e,f,g,h,i)))

##percent chance of voting
anes2012$pctchance <- anes2012$likelypct_whatpct1
anes2012$pctchance[is.na(anes2012$likelypct_whatpct1)]=anes2012$pctlikely_whatpct2[is.na(anes2012$likelypct_whatpct)]
anes2012$pctchance <- zero1(anes2012$pctchance)

anes2012$pk <- rowMeans(data.frame(as.numeric(anes2012$knowl_housemaj)==2,as.numeric(anes2012$knowl_senmaj)==1,as.numeric(anes2012$candrel_dpc)==1,as.numeric(anes2012$candrel_rpc)==5),na.rm=T)

######## Moderators
## Verbal Ability
library(stringr)
nn <- which( colnames(anes2012)=="wordsum_setb" )
wordsum <- as.data.frame(mapply(as.character,anes2012[,nn:(nn+9)]))
trim.leading <- function (x)  sub("^\\s+", "", x)
wordsum1 <- as.data.frame(mapply(function(x)as.factor(str_replace_all(x, "[[:digit:]].", "")),wordsum))
wordsum01 <- as.data.frame(mapply(function(x)car::recode(trim.leading(x),"'Correct'=1;else=0"),wordsum1))

library(ltm)
b <- ltm(wordsum01~z1)
fscores <- factor.scores(b)
newdf <- fscores$score.dat
tobind <- as.data.frame(data.matrix(wordsum01))
tobind$join <- do.call(paste, c(tobind, sep = ""))
newdf$join <- do.call(paste, c(newdf[,1:10], sep = ""))
anes2012$wordsumz1 <-(newdf$z1[match(tobind$join,newdf$join)])

cor(anes2012$wordsumz1,rowMeans(tobind[,1:10]),use="pairwise.complete.obs")
anes2012$wordsumtri <- car::recode(anes2012$wordsumz1,"-5:-.62='Lower';.65:5='Upper';else=NA",as.factor=T)

anes2012$agreeableness <- rowMeans(data.frame(zero1((as.numeric(anes2012$tipi_crit))),zero1((as.numeric(anes2012$tipi_warm)))))
